package com.secure.orm.service.billPay.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.secure.common.util.PayUtil;
import com.secure.orm.enums.PayType;
import com.secure.orm.model.PayOrder;
import com.secure.orm.model.response.ViewData;
import com.secure.orm.service.PayOrderService;
import com.secure.orm.service.PaySourceService;
import com.secure.orm.service.billPay.BillPayService;
import com.secure.orm.thirdPay.BaseThirdPay;

@Service("billPayService")
public class BillPayServiceImpl implements BillPayService {
	protected Log logger = LogFactory.getLog(BillPayServiceImpl.class);

	@Autowired
	private BaseThirdPay baseThirdPay;
	@Autowired
	private PayOrderService payOrderService;
	@Autowired
	private PaySourceService paySourceService;

	public ViewData verifySign(PayOrder payOrder) {
		if (!StringUtils.isEmpty(payOrder.getSubject())) {
			try {
				payOrder.setSubject(URLDecoder.decode(payOrder.getSubject(), "utf-8"));
			} catch (UnsupportedEncodingException e) {
				return new ViewData(-1, "false", "URLDecoder subject error");
			}
		} else {
			return new ViewData(-1, "false", "Subject is empty");
		}
		String text = payOrder.getUserId() + payOrder.getOrderId()
				+ PayUtil.formatNumber(payOrder.getOrderAmount(), null)
				+ payOrder.getSubject();
		String mySign = PayUtil.sign(text, null);
		logger.info("verify:"+mySign);
		if (!mySign.equals(payOrder.getToken()))
			return new ViewData(-1, "false", "verify sign fail");
		return new ViewData(0, "success", null);
	}

	@Override
	public ViewData entryPay(PayOrder payOrder) {
		return verifySign(payOrder);
	}

	@Override
	@Transactional
	public ViewData processPay(PayOrder payOrder) {
		ViewData vd = verifySign(payOrder);
		if (vd.getCode() == -1)
			return vd;
		String className = "";
		if (!StringUtils.isEmpty(payOrder.getPayType())) {
			if ("creditCard".equals(payOrder.getPayType())) {

			} else if ("depositCard".equals(payOrder.getPayType())) {

			} else if ("thirdPay".equals(payOrder.getPayType())) {
				if ("Tenpay".equals(payOrder.getPayWay())) {
					className = PayType.getThirdPayClassName(PayType.Tenpay);
				} else if ("Alipay".equals(payOrder.getPayWay())) {
					className = PayType.getThirdPayClassName(PayType.Alipay);
				} else if ("Bill99".equals(payOrder.getPayWay())) {
					className = PayType.getThirdPayClassName(PayType.Bill99);
				}
			} else if ("onLinePay".equals(payOrder.getPayType())) {

			} else if ("wechatPay".equals(payOrder.getPayType())) {

			}
		}
		logger.info("Begin pay "+JSON.toJSONString(payOrder));
		String buildFrmStr = (String) baseThirdPay.buildRequestForm(payOrder, className);

		// 订单处理
		ViewData view = OrdeProcess(payOrder);
		if (view.getCode() == -1) {
			return view;
		}
		return new ViewData(0, "success", buildFrmStr);
	}

	@Transactional
	public ViewData OrdeProcess(PayOrder payOrder) throws RuntimeException {
		payOrder.setStatus(0);
		payOrder.setSourceId(1);
		payOrder.setCreateTime(new Date());
		PayOrder payos = payOrderService.findByOrderId(payOrder.getOrderId());
		if (payos != null) {
			if (payos.getStatus() > 0) {
				return new ViewData(-1, "fail", "Is Processed");
			}
			payOrder.setId(payos.getId());
			payOrder.setUpdateTime(new Date());
		}
		payOrderService.save(payOrder, true);
		return new ViewData(0, "success", "success");
	}

}
